@echo off
@echo off
echo ================================================================
echo   SetupDatabase
echo      Creates the tables needed by the quickstarts in a single
echo      database named PageFlow_Quickstart.  This database includes
echo      the structures for membership, workflow, and pageflow.
echo.
echo      You can modify this command file to create the database
echo      on a different instance of SQL (for example, to use SQL 
echo      Server 2005). 
echo.
echo      If you do not have .NET 3.0 installed, you will need to 
echo      install .NET 3.0 for this script to work.
echo      If you use a language other than English, you will need to
echo      modify this script where noted.
echo ================================================================
echo.

SETLOCAL

if "%1"=="/?" goto HELP

@REM  -------------------------------------------------------------
@REM  You can change server and database name here.
@REM  -------------------------------------------------------------
SET sqlServer=%1%
if "%1%"=="" set sqlServer=.\sqlexpress

SET dbName=%2%
if "%2%"=="" set dbName=PageFlow_QuickStart

@REM  -------------------------------------------------------------
@REM  You can change the path for the appropriate Windows Workflow 
@REM  Foundation language here.
@REM  -------------------------------------------------------------
SET wfScriptRoot="%windir%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\EN"


@REM  -------------------------------------------------------------
@REM  You can change the path to PageFlow scripts here.
@REM  -------------------------------------------------------------
SET pfScriptRoot="..\..\..\src\PageFlow\scripts"

echo.
echo Creating databases %dbName% on server %sqlServer%

OSQL -S %sqlServer% -E -b -n -d master -Q "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'%dbName%') DROP DATABASE [%dbName%]"
IF ERRORLEVEL 1 GOTO error

OSQL -S %sqlServer% -E -b -n -d master -Q "CREATE DATABASE [%dbName%]"
IF ERRORLEVEL 1 GOTO error

echo.
echo Setting up ASP.NET membership in database '%dbName%'

IF NOT EXIST %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe GOTO MISSINGASPNETSQL

%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe -S %sqlServer% -E -A m -d %dbName% -Q
IF ERRORLEVEL 1 GOTO error

OSQL -S %sqlServer% -d "%dbName%" -E -b -n -i QuickstartMembershipUsers.sql
IF ERRORLEVEL 1 GOTO error

echo.
echo Creating Workflow Persistence in database '%dbName%'

OSQL -S %sqlServer% -d "%dbName%" -E -b -n -i %wfScriptRoot%\SqlPersistenceService_Schema.sql"
IF ERRORLEVEL 1 GOTO error

OSQL -S %sqlServer% -d "%dbName%" -E -b -n -i %wfScriptRoot%\SqlPersistenceService_Logic.sql"
IF ERRORLEVEL 1 GOTO error

echo
echo Creating PageFlow persistence in database '%dbName%'
echo

OSQL -S %sqlServer% -d "%dbName%" -E -b -n -d "%dbName%" -i %pfScriptRoot%\PageFlowInstanceStore.sql"
IF ERRORLEVEL 1 GOTO error

GOTO exit


@REM  -------------------------------------------
@REM  Handle errors
@REM
@REM  Use the following after any call to exit
@REM  and return an error code when errors occur
@REM
@REM  if errorlevel 1 goto :error	
@REM  -------------------------------------------
:error
@ECHO An error occured in SetupDatabase.bat [%errorLevel%]

GOTO EXIT


@REM  -------------------------------------------
@REM  Missing ASP.Net SQL
@REM
@REM  If we cannot locate the aspnet_regsql.exe
@REM  display a message and exit.
@REM  -------------------------------------------
:MISSINGASPNETSQL
@ECHO Could not locate aspnet_regsql.exe.  Verify that .NET version 2.0 is installed.

GOTO EXIT

@REM  -------------------------------------------
@REM  Help
@REM
@REM  Display usage and exit.
@REM  -------------------------------------------
:HELP
echo .
echo Usage: SetupDatabase.cmd

:EXIT